/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.multi.client.components;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import org.macdadi.core.client.domain.DataWriteService;

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class CreateAccountPanel extends Composite
{
    public static final String CSS_CLASS = "macdadi-CreateAccountPanel";

    protected TextBox name;
    protected TextBox email;
    protected PasswordTextBox password;
    protected Button join;
    protected HTML feedback;

    public CreateAccountPanel()
    {
        VerticalPanel vp = new VerticalPanel();
        vp.add(new HTML("<h4>Sign up!</h4>"));

        name = new TextBox();
        email = new TextBox();
        password = new PasswordTextBox();
        join = new Button("Join");
        join.addClickListener(new ClickListener() {
            public void onClick(Widget sender)
            {
                doJoin();
            }
        });
        feedback = new HTML();

        FlexTable table = new FlexTable();
        table.setWidget(0, 0, new HTML("Name"));
        table.setWidget(0, 1, name);
        table.setWidget(1, 0, new HTML("Email"));
        table.setWidget(1, 1, email);
        table.setWidget(2, 0, new HTML("Password"));
        table.setWidget(2, 1, password);
        table.setWidget(3, 0, join);
        table.getFlexCellFormatter().setColSpan(3, 0, 2);
        table.setWidget(4, 0, feedback);
        table.getFlexCellFormatter().setColSpan(4, 0, 2);
        //table.getFlexCellFormatter().setHorizontalAlignment(3, 0, HasAlignment.ALIGN_CENTER);
        vp.add(table);

        initWidget(vp);
        setStyleName(CSS_CLASS);
    }

    protected void doJoin()
    {
        feedback.setText("");

        String nameVal, emailVal, passwordVal;
        nameVal = name.getText().trim();
        emailVal = email.getText().trim();
        passwordVal = password.getText().trim();

        if ( nameVal.length() == 0 || emailVal.length() == 0 || passwordVal.length() == 0 ) {
            doFeedback("All three inputs are required");
            return;
        }

        // TODO: better email address validation required, or better yet: send a confirmation link by email
        if ( emailVal.indexOf("@") < 0 || emailVal.indexOf(".", emailVal.indexOf("@")) < 1 ) {
            doFeedback("A valid email address is required");
            return;
        }

        DataWriteService.App.getInstance().createAccount(emailVal, passwordVal, nameVal, new AsyncCallback() {
            public void onFailure(Throwable caught) {
                GWT.log("Failed to create a new account", caught);
                doFeedback("An error occurred, we were unable to create your account");
            }

            public void onSuccess(Object result) {
                if (((Boolean) result).booleanValue()) {
                    doFeedback("Your account was created, please login");
                } else {
                    doFeedback("Your account was not created, perhaps this email address was already used.");
                }
            }
        });
    }

    protected void doFeedback(String msg)
    {
        feedback.setText(msg);
    }
}
